
package com.xoa.controller.cms;
import com.xoa.model.cms.Site;
import com.xoa.service.cms.RenderingService;
import com.xoa.service.cms.SiteService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;


@Controller
@RequestMapping("/site")
public class SiteController {

    @Resource
    private SiteService siteService;

    @Autowired
    RenderingService renderingService;

    @RequestMapping("/sites")//站点管理
    public String sites() {
        return "app/site/site";
    }

    @RequestMapping("/templateManager")//模板管理
    public String templateManager() {
        return "app/site/templateManager";
    }

    @RequestMapping("/columnManagement")//栏目管理
    public String columnManagement() {
        return "app/site/columnManagement";
    }

    @RequestMapping("/columncout")
    public String columncout() {
        return "app/site/columncout";
    }

    @RequestMapping("/choice")
    public String choice() {
        return "app/site/choice";
    }

    @RequestMapping("/documentManagement")//文档管理
    public String documentManagement() {
        return "app/site/documentManagement";
    }

    @RequestMapping("/documentation")//文档管理
    public String documentation() {
        return "app/site/documentation";
    }

    @RequestMapping("/index")//文档新建弹窗
    public String index() {
        return "app/site/index";
    }

    @RequestMapping("/editor")//文档编辑弹窗
    public String editor() {
        return "app/site/editor";
    }//pageTemplate

    @RequestMapping("/pageTemplate")//首页模板
    public String pageTemplate() {
        return "app/site/pageTemplate";
    }

    /**
     * @作者：张海叶
     * @时间：2018/2/27
     * @介绍: 显示站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/selectSite")
    public ToJson selectSite(){

        return siteService.selectSite();
    }
    /**
     * @作者：张海叶
     * @时间：2018/2/27
     * @介绍: 显示站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/selectSiteById")
    public ToJson selectSiteById(Site site){

        return siteService.selectSiteById(site);
    }

    /**
     * @作者：张海叶
     * @时间：2018/2/27
     * @介绍:添加站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/insertSite")
    public ToJson insertSite(HttpServletRequest request,Site site){

        return siteService.insertSite(request,site);
    }
    /**
     * @作者：张海叶
     * @时间：2018/2/27
     * @介绍:修改站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/upSite")
    public ToJson upSite(Site site){

        return siteService.upSite(site);
    }
    /**
     * @作者：张海叶
     * @时间：2018/2/27
     * @介绍:删除站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/delSite")
    public ToJson delSite(Site site){

        return siteService.delSite(site);
    }

    /**
     * @作者：张海叶
     * @时间：2018/3/12
     * @介绍: 显示站点
     * @参数：sms
     */
    @ResponseBody
    @RequestMapping("/selectSiteByTemp")
    public ToJson selectSiteByTemp(Site site){
        return siteService.selectSiteByTemp(site);
    }

    /**
     * 站点预览
     * @author  杨超
     */

    @RequestMapping("/preview")
    public void preview(HttpServletRequest request, HttpServletResponse response){
        //获取提交的站点id
        int siteId = StringUtils.getInteger(request.getParameter("siteId"),0);
        //获取提交的栏目id
        int channelId = StringUtils.getInteger(request.getParameter("channelId"),0);
        //获取栏目文档id
        int chnlDocId = StringUtils.getInteger(request.getParameter("chnlDocId"),0);

        Map map = request.getParameterMap();

        //获取系统
        String rootPath = request.getSession().getServletContext().getRealPath("/");

        //按照不同的进行页面渲染
        try{
            PrintWriter out = response.getWriter();
            //如果只有站点id，则渲染首页模板
            if(siteId!=0 && channelId == 0 && chnlDocId == 0){
                out.write( renderingService.renderSite(siteId,map,true,rootPath));
            }else if(siteId != 0 && channelId !=0 && chnlDocId ==0){
                //渲染栏目首页
                out.write( renderingService.renderColumn(siteId,channelId,map,true,rootPath));
            }else if(siteId != 0 && channelId !=0 && chnlDocId !=0){
                //渲染文章页面
                out.write(renderingService.renderArticle(siteId,channelId,chnlDocId,map,true,rootPath));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


}
